package com.kehutong.charge.admin;

import com.kehutong.charge.entity.ChargeOrderManage;
import com.kehutong.charge.entity.ChargePayManage;
import com.kehutong.charge.service.ChargeOrderService;
import com.kehutong.charge.service.CommonService;
import com.kehutong.charge.service.ThirdPayManageService;
import com.kehutong.charge.util.CommonUtil;
import com.kehutong.charge.util.enums.ChargeLogEnum;
import com.kehutong.charge.util.enums.ChargeOrderStatusEnum;
import com.kehutong.charge.util.enums.CommonEnum;
import com.kehutong.common.DeptService;
import com.kehutong.common.IpUtils;
import com.kehutong.common.dto.DeptDTO;
import com.kehutong.common.util.Token;
import org.coraframework.authz.HasPermission;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONObject;
import org.coraframework.logger.Logger;
import org.coraframework.logger.LoggerFactory;
import org.coraframework.mvc.MvcAction;
import org.coraframework.mvc.http.Bean;
import org.coraframework.mvc.http.ReqMapping;
import org.coraframework.mvc.http.result.Result;
import org.coraframework.orm.Pool;
import org.coraframework.orm.jdbc.JdbcSession;
import org.coraframework.util.EnumUtil;
import org.coraframework.util.Objects;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @program: kht
 * @description: 订单管理
 * @author: joneron
 * @create: 2021/7/5
 **/
@MvcAction
@ReqMapping("/admin/pay/charge/order")
public class ChargeOrderController {

    private static final Logger logger = LoggerFactory.getLogger(ChargeOrderController.class);

    @Inject
    DeptService deptService;

    @Inject
    ChargeOrderService chargeOrderService;

    @Inject
    ThirdPayManageService payManageService;
    @Inject
    CommonService commonService;

    @Inject
    private JdbcSession jdbcSession;

    @ReqMapping("/page")
    @HasPermission("pay:charge:order:view")
    public Object page(JSONObject jsonObject) {
        return chargeOrderService.findPage(jsonObject);
    }


    @ReqMapping("/page/export")
    @HasPermission("pay:charge:order:view")
    public void pageExport(HttpServletRequest req, HttpServletResponse resp, JSONObject jsonObject) {
        chargeOrderService.pageExport(req,resp,jsonObject);
    }


    @ReqMapping("/list")
    @HasPermission("pay:charge:order:view")
    public Object list(JSONObject jsonObject) {
        return chargeOrderService.findList(jsonObject);
    }

    @Bean(newInstance=true, copy=true)
    @HasPermission("pay:charge:order:edit")
    @ReqMapping("/save")
    public Object save(ChargeOrderManage entity, Token token,JSONObject jsonObject) throws Exception {
        List<String> departList = token.getDepartments();
        if(null != departList && departList.size() > 0){
            logger.error("用户{}数据异常，部门为空！",token.getUsername());

            DeptDTO deptDTO = deptService.getDept(departList.get(0));
            entity.setDepartmentNo(deptDTO.getDepartmentNo());
            entity.setDepartmentNos(deptDTO.getDepartmentNos());
            entity.setDepartmentName(deptDTO.getDepartmentName());
        }

        //生成订单号
        String orderNo = CommonUtil.generateRandom("DD");
        entity.setOrderNo(orderNo);
        entity.setOrderOrigin(CommonEnum.COM_PAY_ROUTINE);
        entity.setPayMethod(CommonEnum.COM_PAY_WECHAT);
        entity.setCreateName(token.getUsername());
        entity.setBatchOrderNo(orderNo);
        entity.setOriginFrom(CommonEnum.COM_PAY_WXPAY_WEB);

        return Result.success(chargeOrderService.save(entity));
    }

    @Bean(copy=true)
    @HasPermission("pay:charge:order:edit")
    @ReqMapping("/update")
    public Object update(ChargeOrderManage entity,Token token,JSONObject jsonObject) throws Exception {


        String stats = jsonObject.getString("orderStatus");
        if(Objects.nonEmpty(stats)){
            entity.setOrderStatus(EnumUtil.getEnum(ChargeOrderStatusEnum.class,stats));
        }

        entity.setUpdateName(token.getUsername());

        return chargeOrderService.update(entity);
    }

    @Bean(copy=true)
    @HasPermission("pay:charge:order:edit")
    @ReqMapping("/updateDesc")
    public Object updateDesc(ChargeOrderManage entity,Token token,JSONObject jsonObject){

        //修改状态
        jdbcSession.updateById(entity);

        //添加操作日志
        return commonService.insetLogger(entity.getOrderNo(),
                ChargeLogEnum.LOG_CHARGE_ORDER_COM.getDesc(),
                "常规订单数据",
                entity.getOrderStatus().getDesc(),
                entity.getOptStatus().getDesc(),"备注订单（备注内容："+entity.getRemark()+")",token.getUsername());
    }

    @Bean(copy=true)
    @HasPermission("pay:charge:order:edit")
    @ReqMapping("/update/submit")
    public Object updatePay(JSONObject jsonObject,HttpServletRequest requset,Token token) throws Exception {

        ChargePayManage cpm = Pool.newInstance(ChargePayManage.class);
        cpm.setCreateIp(IpUtils.getAddr(requset));
        cpm.setCreateName(token.getUsername());
        cpm.setOrderNo(jsonObject.getString("orderNo"));
        cpm.setCompanyNo(token.getCompanyNo());
        cpm.setCompanyName(token.getCompanyName());
        cpm.setPayMethod(EnumUtil.getEnum(CommonEnum.class, jsonObject.getString("payMethod")));
        cpm.setOrderOrigin(EnumUtil.getEnum(CommonEnum.class, jsonObject.getString("orderOrigin")));

        return payManageService.callPayOrder(cpm,jsonObject,token);
    }

    @HasPermission("pay:charge:order:edit")
    @ReqMapping("/delete")
    public Object delete(ChargeOrderManage entity) {

        return chargeOrderService.deleteById(entity.getId());
    }

    @ReqMapping("/get")
    public Object get(ChargeOrderManage entity) {

        return Result.success(entity);
    }

    @ReqMapping("/getObj")
    public Object getObj(JSONObject jsonObject) {

        return Result.success(chargeOrderService.findOrderDetail(jsonObject));
    }

}
